package org.dfl.core.model;

import java.util.HashSet;
import java.util.Set;

import org.dfl.core.utils.Utils;



/**
 * generates a unique ids for object (e.g. to save them in a xml file with corrent ids).
 * @author Piotr Wlodarczyk
 *
 */
public class IdGenerator {
	private static Set<String> ids = new HashSet<String>();
	
	@SuppressWarnings("unchecked")
	public static String getGeneratedId(Class clazz){
		String key = Utils.getClassName(clazz);
		return getGeneratedId(key);
	}

	public static String getGeneratedId(String possibleId) {
		int i = 1;
		while(isRegistered(possibleId+i)){i++;}
		possibleId += i;
		registerId(possibleId);
		return possibleId;
	}
	
	public static void registerId(String id){
		ids.add(id);
	}
	
	public static boolean isRegistered(String id){
		return ids.contains(id);
	}
}
